﻿using System;
using System.Windows.Forms;
using GatewayInspect.Model;
using System.Data;
using GatewayInspect.Common;

namespace GatewayInspect.Modules
{
    public partial class CLGLForm : PopupForm
    {
        private TruckCertInfo _truckInfo;

        public string ID = "";

        public CLGLForm()
        {
            InitializeComponent();
        }

        public CLGLForm(TruckCertInfo info)
            : this()
        {
            this._truckInfo = info;
        }

        private void CLGLForm_Load(object sender, EventArgs e)
        {
            if (_truckInfo == null)
            {
                _truckInfo = new TruckCertInfo();
            }

            this.propertyTruck.SelectedObject = _truckInfo;
            this.propertyTruck.ExpandAllGridItems();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(_truckInfo.TRUCKNO))
            {
                MessageBox.Show("车牌号不能为空！");
                return;
            }
            string strSQL = "select * from T_TRUCK_CERT_INFO where TRUCKNO='" + _truckInfo.TRUCKNO + "'";
            if (!string.IsNullOrEmpty(_truckInfo.ID))
                strSQL += " and ID<>'" + _truckInfo.ID + "'";
            DataTable dt = DataAccess.ExecuteDataTable(strSQL);
            if (dt != null && dt.Rows.Count > 0)
            {
                MessageBox.Show("该车牌号对应的车辆信息已存在！");
                return;
            }
            //if (string.IsNullOrWhiteSpace(_truckInfo.DRIVERID))
            //{
            //    MessageBox.Show("司机身份证号不能为空！");
            //    return;
            //}
            if (TruckCertInfo.SaveToDB(_truckInfo, ref ID))
            {
                this.DialogResult = DialogResult.OK;
            }
            else
            {
                MessageBox.Show("保存失败！");
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        /*
        #region 读身份证

        private IDCradReader reader = null;

        protected override void OnLoad()
        {
            base.OnLoad();
            BeginRead();//启动身份证读卡设备开始读卡 
        }

        protected override void OnClosed()
        {
            base.OnClosed();
            if (reader != null)
            {
                reader.AfterReadInfo -= new IDCradReader.AfterReadInfoHandler(reader_AfterReadInfo);
                reader.CheckHandler();//检查是否还有其他对象读卡
            }
        }

        private void BeginRead()
        {
            if (reader == null)
            {
                reader = IDCradReader.GetInstance();
                reader.AfterReadInfo += new IDCradReader.AfterReadInfoHandler(reader_AfterReadInfo);
            }
            reader.BeginRead();
        }

        private void reader_AfterReadInfo(CardInfo info)
        {
            if (IsActive && info != null && _truckInfo != null)
            {
                //_truckInfo.DRIVERID = info.CardNo;
                //_truckInfo.DRIVERNAME = info.Name;
                //_truckInfo.JSZ_NO = info.CardNo;
                //_truckInfo.JSZ_NAME = info.Name;
                //_truckInfo.CYZGZ_JSYZ.ID = info.CardNo;
                this.propertyTruck.Refresh();
            }
        }

        #endregion
        */
    }
}
